package com.santoker.user.service.service.common.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.santoker.user.service.dao.UserRecordMapper;
import com.santoker.user.service.model.UserRecord;
import com.santoker.user.service.service.common.IUserRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户记录表 服务实现类
 * </p>
 *
 * @author Jiangs
 * @since 2020-04-01
 */
@Service
public class UserRecordServiceImpl extends ServiceImpl<UserRecordMapper, UserRecord> implements IUserRecordService {

    @Override
    public List<UserRecord> findListByUserId(Integer userId) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                    .eq(UserRecord::getUserId, userId)
                    .orderByDesc(UserRecord::getWeight)
                    .orderByDesc(UserRecord::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public List<UserRecord> findShareListByUserId(Integer userId) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .eq(UserRecord::getUserId, userId)
                .eq(UserRecord::getShare, 1)
                .orderByDesc(UserRecord::getWeight)
                .orderByDesc(UserRecord::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public List<UserRecord> findGroupByUserIdShare() {
        return baseMapper.findGroupByUserIdShare();
    }

    @Override
    public List<UserRecord> findGroupByUserIdShareAndSelect() {
        return baseMapper.findGroupByUserIdShareAndSelect();
    }

    @Override
    public List<UserRecord> findByTitleLikeAndShare(String title, Integer share) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .eq(UserRecord::getShare, share)
                .like(StringUtils.isNoneBlank(title), UserRecord::getTitle, title)
                .orderByDesc(UserRecord::getWeight)
                .orderByDesc(UserRecord::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public List<UserRecord> findByTitleLikeAndShareAndSelected(String title, Integer share, Integer selected) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .eq(UserRecord::getShare, share)
                .eq(UserRecord::getSelected, selected)
                .like(StringUtils.isNoneBlank(title), UserRecord::getTitle, title)
                .orderByDesc(UserRecord::getWeight)
                .orderByDesc(UserRecord::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public List<UserRecord> findByAll() {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .orderByDesc(UserRecord::getWeight)
                .orderByDesc(UserRecord::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public List<UserRecord> findByTrainAndType(Integer train, String type) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .eq(UserRecord::getTrain, train)
                .eq(UserRecord::getType, type)
                .orderByDesc(UserRecord::getWeight)
                .orderByDesc(UserRecord::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public UserRecord getUserRecordById(Integer recordId) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .eq(UserRecord::getId, recordId);
        return getOne(queryWrapper, false);
    }

    @Override
    public UserRecord getUserRecordByIdAndUserId(Integer recordId, Integer userId) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .eq(UserRecord::getId, recordId)
                .eq(UserRecord::getUserId, userId);
        return getOne(queryWrapper, false);
    }

    @Override
    public List<UserRecord> findByUserIdAndTitle(Integer userId, String title) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .eq(UserRecord::getTitle, title)
                .eq(UserRecord::getUserId, userId)
                .orderByDesc(UserRecord::getWeight)
                .orderByDesc(UserRecord::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public List<UserRecord> findList(Integer userId, Integer selected, Integer share, Integer train) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .eq(Objects.nonNull(userId), UserRecord::getUserId, userId)
                .eq(Objects.nonNull(selected), UserRecord::getSelected, selected)
                .eq(Objects.nonNull(share), UserRecord::getShare, share)
                .eq(Objects.nonNull(train), UserRecord::getTrain, train)
                .orderByDesc(UserRecord::getWeight)
                .orderByDesc(UserRecord::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public UserRecord getUserRecordByRecordToken(String recordToken) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .eq(UserRecord::getRecordToken, recordToken);
        return getOne(queryWrapper, false);
    }

    @Override
    public Integer getTotalByUserId(Integer userId) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .eq(UserRecord::getUserId, userId);
        return count(queryWrapper);
    }

    @Override
    public List<UserRecord> getTrainListByType(String type) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .eq(UserRecord::getTrain, 1)
                .eq(UserRecord::getType, type)
                .orderByDesc(UserRecord::getCreatedAt)
                .last("limit 0,100");
        return list(queryWrapper);
    }

    @Override
    public List<UserRecord> getListByUserId(Integer userId) {
        LambdaQueryWrapper<UserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserRecord::getIsDelete, 0)
                .eq(UserRecord::getUserId, userId)
                .orderByDesc(UserRecord::getCreatedAt)
                .last("limit 0,100");
        return list(queryWrapper);
    }

    @Override
    public List<UserRecord> findGroupByUserIdSelect() {
        return baseMapper.findGroupByUserIdSelect();
    }

}
